package sync;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import sync.view.AssignmentTableModel;
import sync.model.AssignmentItem;
import org.jdesktop.swingx.MultiSplitLayout;
import sync.model.Assignment;
import sync.model.Assignment.Status;
import sync.view.TableTransferHandler;

/**
 *
 * @author Yarden
 */
public class AssignmentsScreen extends javax.swing.JPanel {

    /** Creates new form AssignmentsScreen */
    public AssignmentsScreen() {
       // initComponents();
        
        init();
        
       // add(multiSplitPane, BorderLayout.CENTER);
    }
   
    public void setAssignments( List<AssignmentItem> inProgress, List<AssignmentItem> done, List<AssignmentItem> onHold) {
        inProgressModel.setAssignments(inProgress);
        doneModel.setAssignments(done);
        onHoldModel.setAssignments(onHold);
    }
    
    public List<AssignmentItem> getAssignments(Assignment.Status status) {
        if (status == Assignment.Status.InProgress) 
            return inProgressModel.getAssignments();
        else if (status == Assignment.Status.Done)
            return doneModel.getAssignments();
        else
            return onHoldModel.getAssignments();
    }
    
    public void setLastSync(String date) {
        if (date.equals(""))
            setStatus("Never synced");
        else
            setStatus("Last synced on "+date);
    }
    
    public void setStatus(String msg) {
        jStatus.setText(msg);
    }
    
    private void setTableHeight(JTable table, int rows) {
        int width = table.getPreferredSize().width;
        int height = rows*table.getRowHeight();
        table.setPreferredScrollableViewportSize(new Dimension(width, height));
    }
    
    private void init() {
        GridBagConstraints gridBagConstraints;

        //setPreferredSize(new Dimension(300, 200));
        setLayout(new GridBagLayout());
        
        // top 

        jPanel1 = new JPanel();
        jPanel1.setMaximumSize(new Dimension(500, 50));
        jPanel1.setLayout(new GridBagLayout());

        jLabel1 = new JLabel();
        jLabel1.setText("User:");
        jPanel1.add(jLabel1, new GridBagConstraints());

        jUsername = new JTextField();
        jUsername.setDragEnabled(false);
        jUsername.setMinimumSize(new java.awt.Dimension(100, 28));
        jUsername.setPreferredSize(new java.awt.Dimension(100, 28));
        jUsername.addKeyListener( new KeyListener() {

            public void keyTyped(KeyEvent ke) {
                if (ke.getKeyChar() == '\n')
                    syncManager.setUsername(jUsername.getText());
                else
                    syncManager.resetUser();
            }

            public void keyPressed(KeyEvent ke) {
                // ignore
            }

            public void keyReleased(KeyEvent ke) {
                // ignore
            }
        });
        jPanel1.add(jUsername, new java.awt.GridBagConstraints());

        jStatus = new JLabel();
        
        syncButton = new JButton();
        syncButton.setText("Sync");
        syncButton.setAlignmentX(1.0F);
        syncButton.setHorizontalAlignment(SwingConstants.LEFT);
        syncButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                syncButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        jPanel1.add(syncButton, gridBagConstraints);

        
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jPanel1.add(jStatus, gridBagConstraints);
        
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        filler = new Box.Filler(new Dimension(30, 0), new Dimension(30, 0), new Dimension(30, 0));
        jPanel1.add(filler, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill =  GridBagConstraints.HORIZONTAL;
        add(jPanel1, gridBagConstraints);
        
        
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        filler = new Box.Filler(new Dimension(30, 0), new Dimension(30, 0), new Dimension(30, 0));
        add(filler, gridBagConstraints);
        /*
         * Tables
         */
        
        TransferHandler handler = new TableTransferHandler(); 
                
        jInProgressTable = new JTable();
        jInProgressTable.setModel(inProgressModel);
        jInProgressTable.setFillsViewportHeight(true);
        jInProgressTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        jInProgressTable.setDragEnabled(true);
        jInProgressTable.setDropMode(DropMode.INSERT_ROWS);
        jInProgressTable.setTransferHandler(handler);
        jInProgressTable.setShowGrid(true);
        setTableHeight(jInProgressTable, 10);
         
        jScrollPane1 = new JScrollPane();
        jScrollPane1.setViewportView(jInProgressTable);
 
        jDoneTable = new JTable();
        jDoneTable.setModel(doneModel);
        jDoneTable.setFillsViewportHeight(true);
        jDoneTable.setDragEnabled(true);
        jDoneTable.setDropMode(DropMode.INSERT_ROWS);
        jDoneTable.setTransferHandler(handler);
        jDoneTable.setShowGrid(true);
        setTableHeight(jDoneTable, 10);
        
        jScrollPane2 = new JScrollPane();
        jScrollPane2.setViewportView(jDoneTable);


        jOnHoldTable = new JTable();
        jOnHoldTable.setModel(onHoldModel);
        jOnHoldTable.setFillsViewportHeight(true);
        jOnHoldTable.setDragEnabled(true);
        jOnHoldTable.setDropMode(DropMode.INSERT_ROWS);
        jOnHoldTable.setTransferHandler(handler);
        jOnHoldTable.setShowGrid(true);
        setTableHeight(jOnHoldTable, 2);
        
        jScrollPane3 = new JScrollPane();
        jScrollPane3.setViewportView(jOnHoldTable);
        // Organize tablels
        
        String layoutDef = "(COLUMN first second third)";
        MultiSplitLayout.Node model = MultiSplitLayout.parseModel(layoutDef);

        multiSplitPane = new org.jdesktop.swingx.MultiSplitPane();
        multiSplitPane.getMultiSplitLayout().setModel(model);
        
       // multiSplitPane.add(jPanel1, "top");
        multiSplitPane.add(jScrollPane1, "first");
        multiSplitPane.add(jScrollPane2, "second");
        multiSplitPane.add(jScrollPane3, "third");
        
        //multiSplitPane.setPreferredSize(model.getBounds().getSize());
        
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1;
        gridBagConstraints.weighty = 1;
        gridBagConstraints.insets = new Insets(10, 10, 10, 10);
        add(multiSplitPane, gridBagConstraints);
     

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    /*
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jButton1 = new javax.swing.JButton();
        list1 = new java.awt.List();
        jFrame1 = new javax.swing.JFrame();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();
        multiSplitPane1 = new org.jdesktop.swingx.MultiSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jUsername = new javax.swing.JTextField();
        syncButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLastsSync = new javax.swing.JLabel();
        jSyncStatus = new javax.swing.JLabel();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(30, 0), new java.awt.Dimension(30, 0), new java.awt.Dimension(30, 0));

        jButton1.setText("jButton1");

        org.jdesktop.layout.GroupLayout jFrame1Layout = new org.jdesktop.layout.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );

        jTable1.setModel(inProgressModel);
        jTable1.setDragEnabled(true);
        jTable1.setDropMode(javax.swing.DropMode.INSERT_ROWS);
        jTable1.setFillsViewportHeight(true);
        jTable1.setShowGrid(true);
        jScrollPane1.setViewportView(jTable1);
        setTableHeight(jTable1, 10);

        jTable2.setModel(doneModel);
        jTable2.setFillsViewportHeight(true);
        jScrollPane2.setViewportView(jTable2);

        jTable3.setModel(onHoldModel);
        jTable3.setColumnSelectionAllowed(true);
        jScrollPane3.setViewportView(jTable3);
        jTable3.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        setPreferredSize(new java.awt.Dimension(300, 200));
        setLayout(new java.awt.GridBagLayout());

        String layoutDef = "(COLUMN top first second third)";
        MultiSplitLayout.Node model = MultiSplitLayout.parseModel(layoutDef);

        multiSplitPane1.getMultiSplitLayout().setModel(model);
        add(multiSplitPane1, new java.awt.GridBagConstraints());

        jPanel1.setMaximumSize(new java.awt.Dimension(500, 50));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("User:");
        jPanel1.add(jLabel1, new java.awt.GridBagConstraints());

        jUsername.setDragEnabled(false);
        jUsername.setMinimumSize(new java.awt.Dimension(100, 28));
        jUsername.setPreferredSize(new java.awt.Dimension(100, 28));
        jUsername.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jUsernameActionPerformed(evt);
            }
        });
        jPanel1.add(jUsername, new java.awt.GridBagConstraints());

        syncButton.setText("Sync");
        syncButton.setAlignmentX(1.0F);
        syncButton.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        syncButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                syncButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        jPanel1.add(syncButton, gridBagConstraints);

        jLabel2.setText("Last sync:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        jPanel1.add(jLabel2, gridBagConstraints);

        jLastsSync.setText("never");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jPanel1.add(jLastsSync, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jPanel1.add(jSyncStatus, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        jPanel1.add(filler1, gridBagConstraints);

        add(jPanel1, new java.awt.GridBagConstraints());
    }// </editor-fold>//GEN-END:initComponents
*/
    
    private void syncButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_syncButtonActionPerformed
        syncManager.setUsername(jUsername.getText());
        syncManager.sync();
    }//GEN-LAST:event_syncButtonActionPerformed

    /*
    private void jUsernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jUsernameActionPerformed
        syncManager.setUsername(jUsername.getText());
    }//GEN-LAST:event_jUsernameActionPerformed
     */ 
    /*
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.Box.Filler filler1;
    private javax.swing.JButton jButton1;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLastsSync;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel jSyncStatus;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    private javax.swing.JTextField jUsername;
    private java.awt.List list1;
    private org.jdesktop.swingx.MultiSplitPane multiSplitPane1;
    private javax.swing.JButton syncButton;
    // End of variables declaration//GEN-END:variables
*/
    
    private javax.swing.Box.Filler filler;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLastsSync;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel jStatus;
    private javax.swing.JTable jInProgressTable;
    private javax.swing.JTable jDoneTable;
    private javax.swing.JTable jOnHoldTable;
    private javax.swing.JTextField jUsername;
    private org.jdesktop.swingx.MultiSplitPane multiSplitPane;
    private javax.swing.JButton syncButton;
    private AssignmentTableModel inProgressModel = new AssignmentTableModel(Assignment.Status.InProgress);
    private AssignmentTableModel doneModel = new AssignmentTableModel(Assignment.Status.Done);
    private AssignmentTableModel onHoldModel = new AssignmentTableModel(Assignment.Status.OnHold);    
    
    private SyncManager syncManager = new SyncManager(this);
}
